package com.yinhd.study.streamStudy;

import com.alibaba.fastjson.JSON;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.IntSummaryStatistics;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author yinhd
 * @program: study220110
 * @description:
 * @create: 2022-01-10 16:03
 * 购物车服务类
 * @date 2022/01/10
 */
public class CartService {
    private static List<Sku> cartSkuList = new ArrayList<Sku>() {
        {
            add(new Sku(1, "无人机", 1000.0, 1, 10.0, SkuCategoryEnum.electronicProducts));
            add(new Sku(2, "衬衫", 20.0, 1, 20.0, SkuCategoryEnum.CLOTHING));
            add(new Sku(3, "手机", 2000.0, 2, 4000.0, SkuCategoryEnum.electronicProducts));
            add(new Sku(4,"飞机",10000.0,1,10000.0,SkuCategoryEnum.electronicProducts));
        }
    };

    /**
     * 每类商品的数量和总金额
     */
//    public void test1(){
//        List<Sku> cartSkuList = this.getCartSkuList();
//        Map<Enum, IntSummaryStatistics> collect = cartSkuList.stream()
//                .collect(Collectors.groupingBy(Sku::getSkuCategory, Collectors.summarizingInt(Sku::getTotalNum)));
//        System.out.println(JSON.toJSONString(collect,true));
//    }

    public static List<Sku> filterSku(List<Sku> skus, CateFilter filter) {
        List<Sku> skuList = new ArrayList<>();
        for (Sku sku : skus) {
            if (filter.test(sku)) {
                skuList.add(sku);
            }
        }
       // Arrays.sort(skuList, (Sku b, Sku a) -> a.getSkuId() - b.getSkuId());


        String[] words = null;
        words[0] = "111";
        words[2] = "222";
        Arrays.sort(words, Comparator.comparingInt(String::length));

        List<String> word = new ArrayList<>();
        word.add("111");
        word.add("222");
//        wordtoArray();
        String string= "111";
        List<String> collect = word.stream().sorted((a, b) -> {
            if (a.length() == b.length()) {
                return a.compareTo(b);
            } else {
                return a.length() - b.length();
            }
        }).collect(Collectors.toList());
        LocalDate day = null;
        Objects.requireNonNull(day,()->  LocalDate.of(2,2,2).toString());
        return skuList;
    }

    public  static  List<Sku> getCartSkuList() {
        return cartSkuList;
    }


}
